/**
 * https://www.cnblogs.com/0328dongbin/p/9134047.html
 * @author wts
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//多态  父类（接口）引用指向是子类对象
public class DBDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException{
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获得连接对象
        String url="jdbc:mysql://172.28.22.100:3306/2017jsj2";
        String username="2017jsj2";
        String password="123abc";
        Connection conn=DriverManager.getConnection(url,username,password);
        //3.获得语句执行平台
        Statement sta=conn.createStatement();//多态
        //4.执行sql语句  insert,update,delete,   select
        
        //String sql="insert into userwts(name,pwd,type) values ('fff','fff',3)";
        //String sql="update user201705050201 set pwd='123'";
        //int row=sta.executeUpdate(sql);
        //System.out.println(row);
        
        //String sql="delete from userwts";
        //int row=sta.executeUpdate(sql);
        //System.out.println(row);
        
        //5.处理结果（仅用于查询）
        ResultSet rs=null;
        //String sql="select * from user201705050201 ";
        String sqlJudgeUserExist="select * from user201705050201 where name='fff'";
        rs=sta.executeQuery(sqlJudgeUserExist);
        int count2=0;
        while(rs.next()){
            count2++;
        }
        if(count2==0){
            System.out.println("没有此用户");
            
        }else{
            String sql="select * from user201705050201 where name='fff' and pwd=321";
            rs=sta.executeQuery(sql);

            int count=0;
            while(rs.next()){
                count++;
                for(int i=0;i<rs.getMetaData().getColumnCount();i++){
                    System.out.print(rs.getObject(i+1)+"  ");
                }
                System.out.println();
            }

            if(count==0){
                System.out.println("密码错误");
            }else if(count>0){
                System.out.println("用户登录成功");
            }
        }
        
        
        
        if(rs!=null){
            rs.close();
        }
        if(sta!=null){
            sta.close();
        }
        if(conn!=null){
            conn.close();
        }
    }
}
